package com.huashi.codeUtil.utils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huashi.codeUtil.entity.PageResponse;

import java.util.List;


/**
 * Controller返回封装类
 * @param <T>
 */
public class JsonResult<T> {
    private final String SUCCESS = "200";
    private final String SUCCESS_MSG = "成功";
    private final String FAIL = "101";
    private String resCode;
    private String msg;
    private T info;


    public String getResCode() {
        return resCode;
    }

    public void setResCode(String resCode) {
        this.resCode = resCode;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getInfo() {
        return info;
    }

    public void setInfo(T info) {
        this.info = info;
    }

    public JsonResult(String resCode,String msg,T info){
        this.resCode = resCode;
        this.msg = msg;
        this.info = info;

    }

    public JsonResult(String resCode,String msg){
        this.resCode = resCode;
        this.msg = msg;
    }

    public static JsonResult success(){
        return new JsonResult("200","成功");
    }

    public static JsonResult success(String msg){
        return new JsonResult("200",msg);
    }

    public static JsonResult successInfo(Object info){
        return new JsonResult("200","成功",info);
    }

    public static JsonResult success(String msg,Object info){
        return new JsonResult("200",msg,info);
    }

    public static JsonResult successPage(IPage page, List records){
        return new JsonResult("200","成功",new PageResponse(page,records));
    }

    public static JsonResult successPage(IPage page){
        return new JsonResult("200","成功",new PageResponse(page));
    }

    public static JsonResult fail(String msg){
        return new JsonResult("101",msg);
    }

    public static JsonResult fail(String errorCode,String msg){
         return new JsonResult(errorCode,msg);
    }

    public String toJsonString(){
        return JSON.toJSONString(this);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
